package xxhui.space.newsclient;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import xxhui.space.newsclient.Bean.User;
import xxhui.space.newsclient.util.MyApplication;

//我的资料页面
public class MeInfoActivity extends AppCompatActivity {

    @BindView(R.id.me_info_toolbar)
    Toolbar toolbar;
    @BindView(R.id.me_info_item_layout_nickname)
    RelativeLayout relativeLayout;
    @BindView(R.id.my_info_head_photo)
    ImageView userPic;
    @BindView(R.id.my_info_phone_text)
    TextView phone;
    @BindView(R.id.my_info_nickname)
    TextView nickName;
    @BindView(R.id.my_info_sex)
    TextView sex;
    @BindView(R.id.my_info_whatisup)
    TextView whatIsUp;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        handler = new Handler();
        setSupportActionBar(toolbar);
        ActionBar actionBar =  getSupportActionBar();
        if(actionBar != null) {
            //取消默认标题，并显示返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MeInfoActivity.this,ModifyNicknameActivity.class),0);
            }
        });

        handler.post(new Runnable() {
            @Override
            public void run() {
                User user = MyApplication.getUser();
                if(user!=null) {
                    Glide.with(MeInfoActivity.this).load(user.getAvatar()).into(userPic);
                    phone.setText(user.getUsername());
                    nickName.setText(user.getNickname());
                    if(user.getSex()==1){
                        sex.setText("男");
                    }else {
                        sex.setText("女");
                    }
                    whatIsUp.setText(user.getWhatisup());
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //点击back键finish当前activity
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0){
            User user = MyApplication.getUser();
            if(user!=null) {
                Glide.with(MeInfoActivity.this).load(user.getAvatar()).into(userPic);
                phone.setText(user.getUsername());
                nickName.setText(user.getNickname());
                if(user.getSex()==1){
                    sex.setText("男");
                }else {
                    sex.setText("女");
                }
                whatIsUp.setText(user.getWhatisup());
            }
        }
    }
}
